package com.example.task4;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private DatabaseHelper dbHelper;
    private EditText etName, etEmail;
    private TextView tvUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbHelper = new DatabaseHelper(this);
        etName = findViewById(R.id.etName);
        etEmail = findViewById(R.id.etEmail);
        Button btnAdd = findViewById(R.id.btnAdd);
        tvUsers = findViewById(R.id.tvUsers);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (validateInput()) {
                    addUser();
                    displayUsers();
                }
            }
        });

        displayUsers();
    }

    private boolean validateInput() {
        if (etName.getText().toString().trim().isEmpty() || 
            etEmail.getText().toString().trim().isEmpty()) {
            Toast.makeText(this, "请填写所有字段", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void addUser() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String name = etName.getText().toString();
        String email = etEmail.getText().toString();
        
        try {
            db.execSQL("INSERT INTO " + DatabaseHelper.TABLE_USERS + " (" + 
                    DatabaseHelper.COLUMN_NAME + ", " + DatabaseHelper.COLUMN_EMAIL + ") VALUES (?, ?)",
                    new String[]{name, email});
            Toast.makeText(this, "用户添加成功", Toast.LENGTH_SHORT).show();
            etName.setText("");
            etEmail.setText("");
        } catch (Exception e) {
            Toast.makeText(this, "添加用户失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    private void displayUsers() {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_USERS, null);
        
        StringBuilder usersList = new StringBuilder("当前用户列表:\n");
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ID));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_NAME));
                String email = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_EMAIL));
                usersList.append("ID: ").append(id)
                        .append(", 名称: ").append(name)
                        .append(", 邮箱: ").append(email)
                        .append("\n");
            } while (cursor.moveToNext());
        } else {
            usersList.append("暂无用户");
        }
        
        tvUsers.setText(usersList.toString());
        cursor.close();
    }
}